#include "ElaWindow.h"
#include "ElaText.h"
#include <QStatusBar>
#include "ElaWidgetTools/ElaMenuBar.h"
#include "ElaWidgetTools/ElaMenu.h"
#include "ElaWidgetTools/ElaStatusBar.h"
#include "ElaWidgetTools/ElaToolBar.h"
#include "ElaWidgetTools/ElaIconButton.h"
#include "ElaWidgetTools/ElaToolButton.h"
#include "ElaStatusSeparator.h"
#include <QToolBar>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QWidget>
#include <QApplication>
#include "ElaApplication.h"
#include "ElaTheme.h"
#include <QDebug>
#include <QDateTime>
#include "ElaLCDNumber.h"

class ExampleWindow : public ElaWindow
{
    Q_OBJECT

  private:
    ElaText* statusBarText;
    ElaText* menuBarText;
    ElaText* toolBarText;
    ElaText* contentText;

  public:
    ExampleWindow(QWidget* parent = nullptr) : ElaWindow(parent)
    {
        QString appName = "RobotMini";
        setWindowTitle(appName);
        setNavigationBarDisplayMode(ElaNavigationType::Compact);
        setUserInfoCardVisible(false);
        setFocusPolicy(Qt::StrongFocus);
        setAppBarHeight(30);
        // 设置窗口按钮标志
        setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint);

        setupUI();
        setupConnections();
    }

  private:
    void setupUI()
    {
        ElaMenuBar* menuBar = new ElaMenuBar(this);
        menuBar->setFixedHeight(30);
        QWidget* customWidget = new QWidget(this);
        QVBoxLayout* customLayout = new QVBoxLayout(customWidget);
        customLayout->setContentsMargins(0, 0, 0, 0);
        customLayout->addWidget(menuBar);
        customLayout->addStretch();
        this->setCustomWidget(ElaAppBarType::MiddleArea, customWidget);
        this->setCustomWidgetMaximumWidth(500);
        menuBar->addElaIconAction(ElaIconType::GamepadModern, "手柄");  // 手柄菜单


        // ============ 工具栏 ============ //


        // 顶部工具栏
        ElaToolBar* toolBar = new ElaToolBar("工具栏", this);
        toolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
        toolBar->setToolBarSpacing(1);
        // toolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
        toolBar->setIconSize(QSize(25, 25));

        int iconSize = toolBar->height() * 0.6;
        ElaIconButton* m_btnCamera = new ElaIconButton(ElaIconType::Camera, iconSize, this);
        m_btnCamera->setMaximumWidth(toolBar->height());
        m_btnCamera->setMinimumHeight(toolBar->height());
        m_btnCamera->setToolTip("主屏幕截图");
        toolBar->addWidget(m_btnCamera);

        ElaToolButton* m_btnDayNight = new ElaToolButton(this);
        m_btnDayNight->setElaIcon(ElaIconType::Sun);
        m_btnDayNight->setToolTip("相机昼夜切换");
        toolBar->addWidget(m_btnDayNight);
        toolBar->addSeparator();
        this->addToolBar(Qt::TopToolBarArea, toolBar);

        //// 左边工具栏
        //ElaToolBar* leftToolBar = new ElaToolBar(tr("左边工具栏"), this);
        //leftToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
        //leftToolBar->setIconSize(QSize(25, 25));
        //// 深度清零按钮
        //ElaToolButton* m_btnResetDepth = new ElaToolButton(this);
        //m_btnResetDepth->setElaIcon(ElaIconType::ArrowsToDottedLine);
        //m_btnResetDepth->setText(tr("深度清零"));

        //// 圈数清零按钮
        //ElaToolButton* m_btnResetTurn = new ElaToolButton(this);
        //m_btnResetTurn->setElaIcon(ElaIconType::ArrowsTurnToDots);
        //m_btnResetTurn->setText(tr("圈数清零"));
        //leftToolBar->addWidget(m_btnResetDepth);
        //leftToolBar->addWidget(m_btnResetTurn);
        //this->addToolBar(Qt::LeftToolBarArea, leftToolBar);


        // 右边工具栏
        ElaToolBar* rightToolBar = new ElaToolBar(tr("右边工具栏"), this);
        rightToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
        rightToolBar->setIconSize(QSize(25, 25));

        // 深度清零按钮
        ElaToolButton* m_btnResetDepth2 = new ElaToolButton(this);
        m_btnResetDepth2->setElaIcon(ElaIconType::ArrowsToDottedLine);
        m_btnResetDepth2->setText(tr("深度清零"));

        // 圈数清零按钮
        ElaToolButton* m_btnResetTurn2 = new ElaToolButton(this);
        m_btnResetTurn2->setElaIcon(ElaIconType::ArrowsTurnToDots);
        m_btnResetTurn2->setText(tr("圈数清零"));

        rightToolBar->addWidget(m_btnResetDepth2);
        rightToolBar->addWidget(m_btnResetTurn2);

        this->addToolBar(Qt::RightToolBarArea, rightToolBar);

        // 底部工具栏
        ElaToolBar* bottomToolBar = new ElaToolBar(tr("底部工具栏"), this);
        bottomToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
        bottomToolBar->setIconSize(QSize(25, 25));

        // 深度清零按钮
        ElaToolButton* m_btnResetDepth3 = new ElaToolButton(this);
        m_btnResetDepth3->setElaIcon(ElaIconType::ArrowsToDottedLine);
        m_btnResetDepth3->setText(tr("深度清零"));

        // 圈数清零按钮
        ElaToolButton* m_btnResetTurn3 = new ElaToolButton(this);
        m_btnResetTurn3->setElaIcon(ElaIconType::ArrowsTurnToDots);
        m_btnResetTurn3->setText(tr("圈数清零"));

        bottomToolBar->addWidget(m_btnResetDepth3);
        bottomToolBar->addWidget(m_btnResetTurn3);

        this->addToolBar(Qt::BottomToolBarArea, bottomToolBar);

        // ============ 状态栏 ============ //

        // 4. 设置中央控件文本
        QWidget* centralWidget = new QWidget(this);
        QVBoxLayout* layout = new QVBoxLayout(centralWidget);

        contentText = new ElaText("中央区域内容文本\n这是一个演示主题切换的示例", centralWidget);
        contentText->setTextStyle(ElaTextType::Title);
        contentText->setAlignment(Qt::AlignCenter);
        contentText->setWordWrap(true);

        // 添加一些样式文本示例
        ElaText* subtitleText = new ElaText("副标题文本 - 主题切换将改变所有文本颜色", centralWidget);
        subtitleText->setTextStyle(ElaTextType::Subtitle);

        ElaText* bodyText = new ElaText(
            "这是一段正文文本，用于演示不同文本样式在主题切换时的颜色变化。"
            "点击标题栏的主题切换按钮可以看到效果。",
            centralWidget);
        bodyText->setTextStyle(ElaTextType::Body);
        bodyText->setWordWrap(true);

        layout->addWidget(contentText);
        layout->addWidget(subtitleText);
        layout->addWidget(bodyText);
        layout->addStretch();

        // 将中央控件添加到窗口
        addCentralWidget(centralWidget);

        // 5. 添加导航节点用于演示
        QString expanderKey;
        addExpanderNode("示例分组", expanderKey, ElaIconType::Folder);

        QString pageKey;
        // addPageNode("示例页面", centralWidget, pageKey, expanderKey, ElaIconType::File);

        //// 6. 设置状态栏文本
        // ElaStatusBar* statusBar = new ElaStatusBar(this);
        ////statusBar->setFixedHeight(30);
        ////statusBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
        ////statusBar->setIconSize(QSize(25, 25));

        // int statusIconSize = statusBar->height() * 0.6;

        ////// 状态文本
        //// m_statusText = new ElaText("初始化成功！", this);
        //// m_statusText->setTextPixelSize(12);
        //// statusBar->addWidget(m_statusText);

        //// 下位机连接状态
        // ElaText* m_mcuStatusText = new ElaText("下位机", this);
        // m_mcuStatusText->setTextPixelSize(12);
        // statusBar->addWidget(m_mcuStatusText);

        // ElaIconButton* m_mcuStatusIcon = new ElaIconButton(ElaIconType::Robot, statusIconSize, this);
        // m_mcuStatusIcon->setMaximumWidth(statusBar->height());
        // m_mcuStatusIcon->setMinimumHeight(statusBar->height());
        // m_mcuStatusIcon->setEnabled(false);
        // statusBar->addWidget(m_mcuStatusIcon);

        //// 游戏手柄连接状态
        // ElaText* m_gamepadStatusText = new ElaText("手柄", this);
        // m_gamepadStatusText->setTextPixelSize(12);
        // statusBar->addWidget(m_gamepadStatusText);

        // ElaIconButton* m_gamepadStatusIcon = new ElaIconButton(ElaIconType::GamepadModern, statusIconSize, this);
        // m_gamepadStatusIcon->setMaximumWidth(statusBar->height());
        // m_gamepadStatusIcon->setMinimumHeight(statusBar->height());
        // m_gamepadStatusIcon->setEnabled(false);
        // statusBar->addWidget(m_gamepadStatusIcon);

        //// 380控制盒连接状态
        // ElaText* m_controlBoxStatusText = new ElaText("控制盒", this);
        // m_controlBoxStatusText->setTextPixelSize(12);
        // statusBar->addWidget(m_controlBoxStatusText);

        // ElaIconButton* m_controlBoxStatusIcon = new ElaIconButton(ElaIconType::Joystick, statusIconSize, this);
        // m_controlBoxStatusIcon->setMaximumWidth(statusBar->height());
        // m_controlBoxStatusIcon->setMinimumHeight(statusBar->height());
        // m_controlBoxStatusIcon->setEnabled(false);
        // statusBar->addWidget(m_controlBoxStatusIcon);

        // this->setStatusBar(statusBar);

        /////////////////
        // ===== 状态栏控件 ===== //
        ElaText* m_statusText;                  // 状态栏文本
        ElaText* m_mcuStatusText;               // 下位机连接状态文本
        ElaIconButton* m_mcuStatusIcon;         // 下位机连接状态图标
        ElaText* m_gamepadStatusText;           // 游戏手柄连接状态文本
        ElaIconButton* m_gamepadStatusIcon;     // 游戏手柄连接状态图标
        ElaText* m_controlBoxStatusText;        // 380控制盒连接状态文本
        ElaIconButton* m_controlBoxStatusIcon;  // 380控制盒连接状态图标

        ElaStatusBar* statusBar = new ElaStatusBar(this);
        statusBar->setFixedHeight(30);
        statusBar->setContentsMargins(10, 0, 0, 0);  // 设置边距
        statusBar->setSizeGripEnabled(false);        // 启用大小调整手柄

        auto createStatusItem = [this, statusBar](ElaText*& textWidget, ElaIconButton*& iconWidget, const QString& text, ElaIconType::IconName icon) {
            QWidget* container = new QWidget(this);
            QHBoxLayout* layout = new QHBoxLayout(container);
            layout->setContentsMargins(15, 1, 15, 2);
            layout->setSpacing(5);

            // 图标
            int statusIconSize = statusBar->height() * 0.7;
            iconWidget = new ElaIconButton(icon, statusIconSize, container);
            iconWidget->setMaximumWidth(statusBar->height());
            iconWidget->setMaximumHeight(statusBar->height());
            iconWidget->setLightIconColor(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
            iconWidget->setDarkIconColor(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
            iconWidget->setLightHoverIconColor(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
            iconWidget->setDarkHoverIconColor(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
            iconWidget->setOpacity(0.7);

            // 文本
            //textWidget = new ElaText(text, container);
            //textWidget->setTextPixelSize(12);
            //textWidget->setTextStyle(ElaTextType::NoStyle);

            //layout->addWidget(textWidget);

            ElaToolButton* textBtn = new ElaToolButton(container);
            textBtn->setToolButtonStyle(Qt::ToolButtonTextOnly);
            textBtn->setText(text);
            //textBtn->setEnabled(false);


            layout->addWidget(textBtn);
            layout->addWidget(iconWidget);
            layout->setAlignment(Qt::AlignCenter);

            return container;
        };

        QWidget* mcuStatusItem = createStatusItem(m_mcuStatusText, m_mcuStatusIcon, "下位机", ElaIconType::Robot);
        QWidget* gamepadStatusItem = createStatusItem(m_gamepadStatusText, m_gamepadStatusIcon, "手柄", ElaIconType::GamepadModern);
        QWidget* controlBoxStatusItem = createStatusItem(m_controlBoxStatusText, m_controlBoxStatusIcon, "控制盒", ElaIconType::Joystick);
        ElaStatusSeparator* separator_1 = new ElaStatusSeparator(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
        statusBar->addWidget(separator_1);
        statusBar->addWidget(mcuStatusItem);
        ElaStatusSeparator* separator_2 = new ElaStatusSeparator(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
        statusBar->addWidget(separator_2);
        statusBar->addWidget(gamepadStatusItem);
        ElaStatusSeparator* separator_3 = new ElaStatusSeparator(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
        statusBar->addWidget(separator_3);
        statusBar->addWidget(controlBoxStatusItem);
        ElaStatusSeparator* separator_4 = new ElaStatusSeparator(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
        statusBar->addWidget(separator_4);

        //m_mcuStatusIcon->setLightIconColor(QColor("#3498DB"));
        //m_mcuStatusIcon->setDarkIconColor(QColor("#5DADE2"));
        //m_mcuStatusIcon->setLightHoverIconColor(QColor("#2E86C1"));
        //m_mcuStatusIcon->setDarkHoverIconColor(QColor("#3498DB"));

        m_mcuStatusIcon->setLightIconColor(QColor("#2980B9"));
        m_mcuStatusIcon->setDarkIconColor(QColor("#3498DB"));
        m_mcuStatusIcon->setLightHoverIconColor(QColor("#2471A3"));
        m_mcuStatusIcon->setDarkHoverIconColor(QColor("#2E86C1"));

        m_mcuStatusIcon->setToolTip("下位机连接成功");
        m_mcuStatusIcon->update();

        m_gamepadStatusIcon->setLightIconColor((ElaThemeColor(ElaThemeType::Light, BasicTextDisable)));
        m_gamepadStatusIcon->setDarkIconColor((ElaThemeColor(ElaThemeType::Light, BasicTextDisable)));
        m_gamepadStatusIcon->setLightHoverIconColor((ElaThemeColor(ElaThemeType::Light, BasicTextDisable)));
        m_gamepadStatusIcon->setDarkHoverIconColor((ElaThemeColor(ElaThemeType::Light, BasicTextDisable)));
        m_gamepadStatusIcon->setToolTip("下位机连接断开");
        m_gamepadStatusIcon->setOpacity(0.7);
        m_gamepadStatusIcon->update();

        ElaLCDNumber* _lcdNumber{nullptr};

        _lcdNumber = new ElaLCDNumber(this);
        _lcdNumber->setIsUseAutoClock(true);
        _lcdNumber->setIsTransparent(true);
        //_lcdNumber->setAutoClockFormat("hh:mm:ss");
        ElaStatusSeparator* separator_5 = new ElaStatusSeparator(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
        statusBar->addPermanentWidget(separator_5);
        statusBar->addPermanentWidget(_lcdNumber);  // 靠右

        this->setStatusBar(statusBar);
    }

    void setupConnections()
    {
        // 监听主题变化，更新文本颜色提示
        // connect(eTheme, &ElaTheme::themeModeChanged, this, [this](ElaThemeType::ThemeMode mode) {
        //    QString themeName = (mode == ElaThemeType::Light) ? "浅色" : "深色";
        //
        //    // 更新状态栏文本显示当前主题
        //    statusBarText->setText(QString("状态栏文本 - 当前主题: %1").arg(themeName));
        //
        //    // 更新中央文本
        //    contentText->setText(QString("中央区域内容文本\n当前主题: %1模式\n文本颜色已自动适应主题").arg(themeName));
        //});

        // 监听导航节点点击
        connect(this, &ElaWindow::navigationNodeClicked, this, [](ElaNavigationType::NavigationNodeType nodeType, QString nodeKey) { qDebug() << "导航节点被点击:" << nodeType << nodeKey; });

        // 监听用户信息卡片点击
        connect(this, &ElaWindow::userInfoCardClicked, this, []() { qDebug() << "用户信息卡片被点击"; });
    }
};

// 使用示例
int main(int argc, char* argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);  // 启用高 DPI (界面布局和几何)
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);                                               //  启用自动高 DPI 缩放 (图标和图片)
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);  // 使用精确的缩放因子，不进行四舍五入
#else
    // 根据实际屏幕缩放比例更改
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);

    eApp->init();  // 初始化资源、字体等

    ExampleWindow window;
    // window.setWindowTitle("ElaWindow 主题切换示例");
    window.resize(1200, 800);
    window.moveToCenter();
    window.show();

    return app.exec();
}

#include "ExampleWindow.moc"